import smtplib
from email.mime.text import MIMEText
from email.header import Header

#https://blog.csdn.net/qq_40243750/article/details/126032582
class EmailUtils:
    def __init__(self, host, user, password,port=25):
        """
        host：邮件服务器地址
        port：邮件服务器端口
        user：自己邮箱账户名
        password：自己邮箱账户的密码（注意是授权码，不是邮箱官网的登录密码）
        """
        self.user = user
        self.password = password
        self.smtp = smtplib.SMTP()  # 创建 SMTP 对象
        self.smtp.connect(host=host, port=port)  # 链接到服务器
        self.smtp.login(user=self.user, password=self.password)  # 登录自己邮箱账号

    def send(self, title, Context, to_addrs):
        """
        Context：邮件正文
        Subject：邮件主题
        to_addrs: 收件人邮箱地址
        """
        message = MIMEText(Context, 'plain', 'utf-8')
        message['From'] = Header(self.user)
        message['Subject'] = Header(title, 'utf-8')
        self.smtp.sendmail(from_addr=self.user, to_addrs=to_addrs, msg=message.as_string())


if __name__ == '__main__':
    emailUtils = EmailUtils(host="smtp.163.com", user="testfan2@163.com", password="MSQOTDVQPMKSRZZS")
    # 发送一封邮件
    emailUtils.send(title="test", Context="python test", to_addrs="279313259@qq.com")